home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 12158 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  1.3 KB

  1. Path: news.NetVision.net.il!news
  2. From: "Poul A. Costinsky" <PoulACost@msn.com>
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: overload + operator question
  5. Date: Mon, 18 Mar 1996 12:29:46 +0200
  6. Organization: NetVision LTD.
  7. Message-ID: <314D3B1A.11E4@msn.com>
  8. References: <1996Mar14.133124.12847@driftwood.cray.com>
  9. NNTP-Posting-Host: 194.90.116.11
  10. Mime-Version: 1.0
  11. Content-Type: text/plain; charset=us-ascii
  12. Content-Transfer-Encoding: 7bit
  13. X-Mailer: Mozilla 2.0GoldB1 (Win95; I)
  14. CC: vb@cray.com
  15.  
  16. >>  o3 = o2 + 100;
  17. >>// will be flagged as 'illegal' unless friend ob + ob defined:
  18. >>  o3 = 100 + o2;The answer is in every serious book about C++ (did you read 
  19. Stroustroup's ?).
  20. What you are actually doing is calling operator+ for 100, which
  21. is integer and therefore cannot accept parameter coord.
  22. If operator+ is a member function of class coord, then
  23. "this", which is the first parameter, must be of class coord.
  24. When operator+ is global function (not necessary friend) then
  25. constructor coord(int) can be applied and 
  26.     o3 = 100 + o2;
  27. becames
  28.     o3 = coord(100) + o2;
  29.  
  30. -- 
  31. Hope this helps.
  32. Regards,
  33. Poul.
  34.   ("`-''-/").___..--''"`-._    ~~~~~~~~~~Poul A. Costinsky~~~~~~~~~~
  35.     (`6_ 6  )   `-.  (     ).`-.__.`)      PoulACost@msn.com
  36.      (_Y_.)'  ._   )  `._ `. ``-..-'
  37.    _..`--'_..-_/  /--'_.' ,'
  38.   (il).-''  (li).'  ((!.-
  39.